package com.lk.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lk.base.result.Result;
import com.lk.web.entity.SysRole;
import com.lk.web.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @program: lk-security-parent
 * @description: 角色管理
 * @author: Aspirin
 * @create: 2020-06-14 22:58
 */
@Controller
@RequestMapping("/role")
public class SysRoleController {
  private static final String HTML_PREFIX = "system/role/";
  @Autowired SysRoleService sysRoleService;

  /** 跳转角色管理页面 */
  @PreAuthorize("hasAuthority('sys:role')")
  @GetMapping(value = {"/", ""})
  public String role() {
    return HTML_PREFIX + "role-list";
  }

  /**
   * 分页:角色列表数据
   *
   * @param page 分页
   * @param sysRole 角色实体
   * @return json
   */
  @PreAuthorize("hasAuthority('sys:role:list')")
  @PostMapping("/page")
  @ResponseBody
  public Result page(Page<SysRole> page, SysRole sysRole) {
    page.addOrder(OrderItem.desc("create_date"));
    Page<SysRole> rolePage = sysRoleService.selectPage(page, sysRole);
    return Result.ok(rolePage);
  }

  /**
   * 分页:角色列表数据 【无需写XML版本】
   *
   * @param page 分页
   * @param sysRole 角色实体
   * @return json
   */
  @PreAuthorize("hasAuthority('sys:role:list')")
  @PostMapping("/page2")
  @ResponseBody
  public Result page2(Page<SysRole> page, SysRole sysRole) {
    // 排序
    page.addOrder(OrderItem.asc("name"));
    LambdaQueryWrapper<SysRole> qw = new LambdaQueryWrapper<>();
    // 高级搜索
    if (sysRole != null && sysRole.getName() != null) {
      qw.like(SysRole::getName, sysRole.getName());
    }
    Page<SysRole> rolePage = sysRoleService.selectPage2(page, qw);
    return Result.ok(rolePage);
  }

  /**
   * 跳转新增或修改页面
   *
   * @param id 角色id
   * @param model model
   */
  @PreAuthorize("hasAnyAuthority('sys:role:add', 'sys:role:edit')")
  @GetMapping(value = {"/form", "/form/{id}"})
  public String form(@PathVariable(required = false) Long id, Model model) {
    // 通过角色id查询对应的角色信息和权限信息
    SysRole role = sysRoleService.findById(id);
    model.addAttribute("role", role);
    return HTML_PREFIX + "role-form";
  }

  /**
   * 提交新增或者修改的数据
   *
   * @param sysRole sysRole
   */
  @PreAuthorize("hasAnyAuthority('sys:role:add', 'sys:role:edit')")
  @RequestMapping(
      method = {RequestMethod.POST, RequestMethod.PUT},
      value = "")
  public String saveOrUpdate(SysRole sysRole) {
    sysRoleService.saveOrUpdate(sysRole);
    return "redirect:/role";
  }
  /**
   * 删除角色
   *
   * @param id 角色id
   * @return json
   */
  @PreAuthorize("hasAuthority('sys:role:delete')")
  @DeleteMapping("/{id}")
  @ResponseBody
  public Result deleteById(@PathVariable("id") Long id) {
    sysRoleService.deleteById(id);
    return Result.ok();
  }
}
